def nextGreaterElement(nums1, nums2):
    res = []
    stack = nums2.copy()
    for num in nums1:
        temp = []
        is_found = True
        max = -1
        while len(stack) != 0 and is_found:
            top = stack.pop()
            if top > num:
                max = top
            elif top == num:
                is_found = False
            temp.append(top)
        res.append(max)
        while len(temp) != 0:
            stack.append(temp.pop())
    return res


nums1 = [4, 1, 2]
nums2 = [1, 3, 4, 2]
print(nextGreaterElement(nums1, nums2))  # [-1,3,1]
